package org.jeecg.modules.workflow.convert.task;

import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.task.task.BpmTaskDonePageItemRespVO;
import org.jeecg.common.workflow.vo.task.task.BpmTaskRespVO;
import org.jeecg.common.workflow.vo.task.task.BpmTaskTodoPageItemRespVO;
import org.jeecg.modules.workflow.entity.BpmTaskExt;
import org.jeecg.modules.workflow.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import org.springframework.stereotype.Service;


@Service
public class BpmTaskConvertImpl implements BpmTaskConvert {

    @Override
    public BpmTaskTodoPageItemRespVO convert1(Task bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskTodoPageItemRespVO bpmTaskTodoPageItemRespVO = new BpmTaskTodoPageItemRespVO();

        bpmTaskTodoPageItemRespVO.setSuspensionState( convertSuspendedToSuspensionState( bean.isSuspended() ) );
        bpmTaskTodoPageItemRespVO.setId( bean.getId() );
        bpmTaskTodoPageItemRespVO.setName( bean.getName() );
        bpmTaskTodoPageItemRespVO.setClaimTime( bean.getClaimTime() );
        bpmTaskTodoPageItemRespVO.setCreateTime( bean.getCreateTime() );

        return bpmTaskTodoPageItemRespVO;
    }

    @Override
    public BpmTaskDonePageItemRespVO convert2(HistoricTaskInstance bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskDonePageItemRespVO bpmTaskDonePageItemRespVO = new BpmTaskDonePageItemRespVO();

        bpmTaskDonePageItemRespVO.setId( bean.getId() );
        bpmTaskDonePageItemRespVO.setName( bean.getName() );
        bpmTaskDonePageItemRespVO.setClaimTime( bean.getClaimTime() );
        bpmTaskDonePageItemRespVO.setCreateTime( bean.getCreateTime() );
        bpmTaskDonePageItemRespVO.setEndTime( bean.getEndTime() );
        bpmTaskDonePageItemRespVO.setDurationInMillis( bean.getDurationInMillis() );

        return bpmTaskDonePageItemRespVO;
    }

    @Override
    public BpmTaskExt convert(org.flowable.task.api.Task bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskExt bpmTaskExtDO = new BpmTaskExt();

        bpmTaskExtDO.setTaskId( bean.getId() );
        if ( bean.getAssignee() != null ) {
            bpmTaskExtDO.setAssigneeUserId( bean.getAssignee());
        }
        bpmTaskExtDO.setCreateTime( bean.getCreateTime() );
        bpmTaskExtDO.setName( bean.getName() );
        bpmTaskExtDO.setProcessInstanceId( bean.getProcessInstanceId() );
        bpmTaskExtDO.setProcessDefinitionId( bean.getProcessDefinitionId() );

        return bpmTaskExtDO;
    }

    @Override
    public BpmTaskRespVO convert3(HistoricTaskInstance bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskRespVO bpmTaskRespVO = new BpmTaskRespVO();

        bpmTaskRespVO.setDefinitionKey( bean.getTaskDefinitionKey() );
        bpmTaskRespVO.setId( bean.getId() );
        bpmTaskRespVO.setName( bean.getName() );
        bpmTaskRespVO.setClaimTime( bean.getClaimTime() );
        bpmTaskRespVO.setCreateTime( bean.getCreateTime() );
        bpmTaskRespVO.setEndTime( bean.getEndTime() );
        bpmTaskRespVO.setDurationInMillis( bean.getDurationInMillis() );

        return bpmTaskRespVO;
    }

    @Override
    public BpmTaskRespVO.User convert3(LoginUser bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskRespVO.User user = new BpmTaskRespVO.User();
        user.setId( bean.getId() );
        user.setNickname( bean.getRealname());
        /*if(bean.getDept() != null){
            user.setDeptId( bean.getDept().getDeptId() );
        }*/

        return user;
    }

    @Override
    public void copyTo(BpmTaskExt from, BpmTaskDonePageItemRespVO to) {
        if ( from == null ) {
            return;
        }

        to.setName( from.getName() );
        to.setCreateTime( from.getCreateTime() );
        to.setEndTime( from.getEndTime() );
        to.setResult( from.getResult() );
        to.setComment( from.getRemark() );
    }

    @Override
    public BpmTaskTodoPageItemRespVO.ProcessInstance convert(ProcessInstance processInstance, LoginUser startUser) {
        if ( processInstance == null && startUser == null ) {
            return null;
        }

        BpmTaskTodoPageItemRespVO.ProcessInstance processInstance1 = new BpmTaskTodoPageItemRespVO.ProcessInstance();

        if ( processInstance != null ) {
            processInstance1.setId( processInstance.getId() );
            processInstance1.setName( processInstance.getName() );
            if ( processInstance.getStartUserId() != null ) {
                processInstance1.setStartUserId(processInstance.getStartUserId());
            }
            processInstance1.setProcessDefinitionId( processInstance.getProcessDefinitionId() );
        }
        if ( startUser != null ) {
            processInstance1.setStartUserNickname( startUser.getRealname() );
        }

        return processInstance1;
    }

    @Override
    public BpmTaskTodoPageItemRespVO.ProcessInstance convert(HistoricProcessInstance processInstance, LoginUser startUser) {
        if ( processInstance == null && startUser == null ) {
            return null;
        }

        BpmTaskTodoPageItemRespVO.ProcessInstance processInstance1 = new BpmTaskTodoPageItemRespVO.ProcessInstance();

        if ( processInstance != null ) {
            processInstance1.setId( processInstance.getId() );
            processInstance1.setName( processInstance.getName() );
            if ( processInstance.getStartUserId() != null ) {
                processInstance1.setStartUserId( processInstance.getStartUserId() );
            }
            processInstance1.setProcessDefinitionId( processInstance.getProcessDefinitionId() );
        }
        if ( startUser != null ) {
            processInstance1.setStartUserNickname( startUser.getRealname() );
        }

        return processInstance1;
    }

    @Override
    public void copyTo(ProcessInstance from, BpmMessageSendWhenTaskCreatedReqDTO to) {
        if ( from == null ) {
            return;
        }

        to.setProcessInstanceName( from.getName() );
        to.setProcessInstanceId( from.getProcessInstanceId() );
        if ( from.getStartUserId() != null ) {
            to.setStartUserId( from.getStartUserId());
        }
        else {
            to.setStartUserId( null );
        }
    }

    @Override
    public void copyTo(LoginUser from, BpmMessageSendWhenTaskCreatedReqDTO to) {
        if ( from == null ) {
            return;
        }

        to.setStartUserId( from.getId());
        to.setStartUserNickname( from.getRealname() );
    }

    @Override
    public void copyTo(org.flowable.task.api.Task task, BpmMessageSendWhenTaskCreatedReqDTO to) {
        if ( task == null ) {
            return;
        }

        to.setTaskId( task.getId() );
        to.setTaskName( task.getName() );
        if ( task.getAssignee() != null ) {
            to.setAssigneeUserId( task.getAssignee());
        }
        else {
            to.setAssigneeUserId( null );
        }
        to.setProcessInstanceId( task.getProcessInstanceId() );
    }
}
